home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / imagemap / imap_file.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-12-16  |  4.9 KB  |  179 lines

  1. /*
  2.  * This is a plug-in for the GIMP.
  3.  *
  4.  * Generates clickable image maps.
  5.  *
  6.  * Copyright (C) 1998-1999 Maurits Rijk  lpeek.mrijk@consunet.nl
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21.  *
  22.  */
  23.  
  24. #include "config.h"
  25.  
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28.  
  29. #include "libgimp/stdplugins-intl.h"
  30. #include "libgimp/gimpui.h"
  31.  
  32. #include "imap_default_dialog.h"
  33. #include "imap_file.h"
  34. #include "imap_main.h"
  35. #include "imap_table.h"
  36.  
  37. static void
  38. open_cb(GtkWidget *widget, gpointer data)
  39. {
  40.    char *filename;
  41.    struct stat buf;
  42.    int err;
  43.  
  44.    filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(data));
  45.    err = stat(filename, &buf);
  46.    if (!err && (buf.st_mode & S_IFREG)) {
  47.       gtk_widget_hide((GtkWidget*) data);
  48.       load(filename);
  49.    } else {
  50.       do_file_error_dialog(_("Error opening file"), filename);
  51.    }
  52. }
  53.  
  54. void
  55. do_file_open_dialog(void)
  56. {
  57.    static GtkWidget *dialog = NULL;
  58.  
  59.    if (!dialog) {
  60.       dialog = gtk_file_selection_new(_("Load Imagemap"));
  61.       gimp_dialog_set_icon (GTK_WINDOW (dialog));
  62.       gtk_signal_connect (GTK_OBJECT (dialog), "delete_event",
  63.                           GTK_SIGNAL_FUNC(gtk_widget_hide), NULL);
  64.       gtk_signal_connect_object(
  65.      GTK_OBJECT(GTK_FILE_SELECTION(dialog)->cancel_button),
  66.      "clicked", GTK_SIGNAL_FUNC(gtk_widget_hide), GTK_OBJECT(dialog));
  67.       gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(dialog)->ok_button),
  68.              "clicked", GTK_SIGNAL_FUNC(open_cb), dialog);
  69.    }
  70.    gtk_widget_show(dialog);
  71. }
  72.  
  73. static void
  74. really_overwrite(gpointer data)
  75. {
  76.    gtk_widget_hide((GtkWidget*) data);
  77.    save_as(gtk_file_selection_get_filename(GTK_FILE_SELECTION(data)));
  78. }
  79.  
  80. static void
  81. do_file_exists_dialog(gpointer data)
  82. {
  83.    static DefaultDialog_t *dialog = NULL;
  84.  
  85.    if (!dialog) {
  86.       dialog = make_default_dialog(_("File exists!"));
  87.       default_dialog_hide_apply_button(dialog);
  88.       default_dialog_set_ok_cb(dialog, really_overwrite, data);
  89.       default_dialog_set_label(
  90.      dialog,
  91.      _("File already exists.\n"
  92.      "  Do you really want to overwrite?  "));
  93.    }
  94.    default_dialog_show(dialog);
  95. }
  96.  
  97. static void
  98. save_cb(GtkWidget *widget, gpointer data)
  99. {
  100.    char *filename;
  101.    struct stat buf;
  102.    int err;
  103.  
  104.    filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(data));
  105.    err = stat(filename, &buf);
  106.    if (err) {
  107.       gtk_widget_hide((GtkWidget*) data);
  108.       save_as(filename);
  109.    } else {            /* File exists */
  110.       if (buf.st_mode & S_IFREG) {
  111.      do_file_exists_dialog(data);
  112.       } else {
  113.                 /* Fix me! */
  114.       }
  115.    }
  116. }
  117.  
  118. void
  119. do_file_save_as_dialog(void)
  120. {
  121.    static GtkWidget *dialog = NULL;
  122.  
  123.    if (!dialog) {
  124.       dialog = gtk_file_selection_new(_("Save Imagemap"));
  125.       gimp_dialog_set_icon (GTK_WINDOW (dialog));
  126.       gtk_signal_connect (GTK_OBJECT (dialog), "delete_event",
  127.                           GTK_SIGNAL_FUNC(gtk_widget_hide), NULL);
  128.       gtk_signal_connect_object(
  129.      GTK_OBJECT(GTK_FILE_SELECTION(dialog)->cancel_button),
  130.      "clicked", GTK_SIGNAL_FUNC(gtk_widget_hide), GTK_OBJECT(dialog));
  131.       gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(dialog)->ok_button),
  132.              "clicked", GTK_SIGNAL_FUNC(save_cb), dialog);
  133.    }
  134.    gtk_widget_show(dialog);
  135. }
  136.  
  137. typedef struct {
  138.    DefaultDialog_t *dialog;
  139.    GtkWidget *error;
  140.    GtkWidget *filename;
  141. } FileErrorDialog_t;
  142.  
  143. static FileErrorDialog_t*
  144. create_file_error_dialog()
  145. {
  146.    FileErrorDialog_t *file_dialog = g_new(FileErrorDialog_t, 1);
  147.    DefaultDialog_t *dialog;
  148.    GtkWidget *table;
  149.  
  150.    file_dialog->dialog = dialog = make_default_dialog(_("Error"));
  151.    default_dialog_hide_apply_button(dialog);
  152.    default_dialog_hide_cancel_button(dialog);
  153.  
  154.    table = gtk_table_new(2, 1, FALSE);
  155.    gtk_container_set_border_width(GTK_CONTAINER(table), 10);
  156.    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog->dialog)->vbox), table, 
  157.               TRUE, TRUE, 10);
  158.    gtk_widget_show(table);
  159.  
  160.    file_dialog->error = create_label_in_table(table, 0, 0, "");
  161.    file_dialog->filename = create_label_in_table(table, 1, 0, "");
  162.  
  163.    return file_dialog;
  164. }
  165.  
  166. void
  167. do_file_error_dialog(const char *error, const char *filename)
  168. {
  169.    static FileErrorDialog_t *dialog = NULL;
  170.  
  171.    if (!dialog)
  172.       dialog = create_file_error_dialog();
  173.  
  174.    gtk_label_set_text(GTK_LABEL(dialog->error), error);
  175.    gtk_label_set_text(GTK_LABEL(dialog->filename), filename);
  176.  
  177.    default_dialog_show(dialog->dialog);
  178. }
  179.